/* Copyright (c) 2004-2024 Peigen.info. All rights reserved. */

package info.peigen.hotpot.business.fee.core.service.processor.active;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import info.peigen.hotpot.business.account.core.domain.account.Account;
import info.peigen.hotpot.business.fee.core.domain.Fee;
import info.peigen.hotpot.business.fee.core.domain.FeeLog;
import info.peigen.hotpot.business.fee.core.domain.FeeRule;
import info.peigen.hotpot.business.fee.core.repository.entity.FeeRuleEntity;
import info.peigen.hotpot.business.fee.core.service.processor.base.AbstractFeeServiceProcessor;
import info.peigen.hotpot.business.fee.facade.command.order.FeeOrder;
import info.peigen.hotpot.business.fee.facade.enums.FeeProductCode;
import info.peigen.hotpot.common.core.result.ResultCode;
import info.peigen.hotpot.common.service.base.exception.BusinessException;
import info.peigen.hotpot.common.service.base.processor.Processor;
import info.peigen.hotpot.common.service.base.processor.ProcessorServiceContext;
import org.noear.solon.annotation.Component;


/**
 * <b>(ActiveCalculateFeeProcessor)</b>
 *
 * @author Peigen
 * @version 1.0.0
 * @since 2022/6/3
 */
@Component
public class ActiveCalculateFeeProcessor extends AbstractFeeServiceProcessor implements Processor {
    @Override
    public Integer level() {
        return 11;
    }

    @Override
    public void execute(ProcessorServiceContext serviceContext) {
        FeeOrder order     = (FeeOrder) serviceContext.getOrder();
        Account  account   = null;
        String   accountId = null;
        if (StrUtil.isNotEmpty(order.getCustomerId())) {
            account = accountRepository.active(order.getCustomerId());
            Assert.notNull(account, () -> new BusinessException(ResultCode.FAILURE, "客户账户不存在"));
            accountId = account.getAccountId();
            serviceContext.getExtend().put("account", account);
        }
        FeeRule rule = null;
        if (FeeProductCode.Recharge == order.getCode()) {
            FeeRuleEntity entity = feeRepository.getFeeRuleRepository().queryFeeRule(order.getStationParentId(), order.getPartnerId(), accountId, FeeProductCode.Recharge, order.getTransAmount().getCurrency());
            rule = feeRuleRepository.active(entity);
        } else {
            rule = feeRuleRepository.active(account, order.getCode());
        }

        Fee fee = active(order, rule);

        serviceContext.setDomain(fee);
    }

    private Fee active(FeeOrder order, FeeRule rule) {
        return Fee.builder()
                .action(order.getAction())
                .code(order.getCode())
                .tradeId(order.getTradeId())
                .transAmount(order.getTransAmount())
                .memo(order.getMemo())
                .feeLog(FeeLog.builder().action(order.getAction())
                        .account(rule.getAccount())
                        .memo(order.getMemo())
                        .tradeId(order.getTradeId())
                        .code(order.getCode()).build()
                        .setRule(rule))
                .rule(rule)
                .calculateAccount(rule.getAccount())
                .billingAccount(rule.getBillingAccount())
                .chargeAccount(rule.getPayerAccount())
                .feeAccount(rule.getPayeeAccount())
                .build();
    }

}